package com.sheng.whyclass.person.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.LoginDataHelper;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.person.event.StudentLoginEvent;

import de.greenrobot.event.EventBus;

public class StudentLoginActivity extends Activity implements View.OnClickListener {

    private TextView title;
    private EditText studentId;
    private EditText studentPass;
    private Button studentLoginBtn;
    private TextView teacherLoginText;
    private TextView studentRegisterText;

    private String student_id;
    private String student_pass;
    private boolean islogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        useCache();
        EventBus.getDefault().register(this);
        initView();
    }

    private void useCache() {
        islogin = LoginDataHelper.isLogin();
        if(islogin){
            startActivity(new Intent(StudentLoginActivity.this, StudentMainActivity.class));
            findStudentInfo();
        }
    }


    private void initView() {
        title = (TextView) findViewById(R.id.title);
        studentId = (EditText) findViewById(R.id.student_name);
        studentPass = (EditText) findViewById(R.id.student_pass);
        studentLoginBtn = (Button) findViewById(R.id.student_login_btn);
        studentLoginBtn.setOnClickListener(this);
        teacherLoginText = (TextView) findViewById(R.id.teacher_login_text);
        teacherLoginText.setOnClickListener(this);
        studentRegisterText = (TextView) findViewById(R.id.student_register_text);
        studentRegisterText.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.teacher_login_text:
                startActivity(new Intent(StudentLoginActivity.this, TeacherLoginActivity.class));
                finish();
                break;
            case R.id.student_login_btn:
                student_id = studentId.getText().toString().trim();
                student_pass = studentPass.getText().toString().trim();
                if (TextUtils.isEmpty(student_id) || TextUtils.isEmpty(student_pass)) {
                    CustomToast.showLongToast(getString(R.string.para_null));
                    return;
                }
                login_student(student_id, student_pass);
                break;

            case R.id.student_register_text:
                startActivity(new Intent(StudentLoginActivity.this, StudentRegisterActivity.class));
                break;
        }
    }

    private void login_student(final String student_name, final String student_pass) {

        StringRequest request = HttpProcessManager.getInstance().loginStudent(Constant.HOST_STUDENT_LOGIN, student_name, student_pass);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);

    }

    public void onEventMainThread(StudentLoginEvent event) {


        int code = event.code;
        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            if (code == Constant.CODE_SUCCESS) {
                startActivity(new Intent(StudentLoginActivity.this, StudentMainActivity.class));
                findStudentInfo();
//               findStudentWork();
                finish();
            }
            CustomToast.showToast(event.info);
        }

    }


    private void findStudentInfo() {
        StringRequest request = HttpProcessManager.getInstance().findStudentInfo(Constant.HOST_STUDENT_FIND, student_id);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);

    }
}
